<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class group_model extends CI_Model
{
    public function get_group_details($groupType, $groupID) {
        if ($groupType == 'group') {
            $this->db->select('
                group.group_id,
                group.group_name,
                group.group_description,
                user.user_id as owner_id,
                user.first_name as owner_first,
                user.last_name as owner_last,
                college.college_name as college,
                group_type.group_type_description as group_type
            ');

            $this->db->join('user', 'user.user_id = group.group_owner');
            $this->db->join('college', 'college.college_id = group.group_college');
            $this->db->join('group_type', 'group_type.group_type_id = group.group_type');
            $query = $this->db->get_where('group', array('group_id' => $groupID));
        }
        else {
            $query = $this->db->get_where($groupType, array($groupType . '_id' => $groupID));
        }

        if ($query->num_rows() != 1) return FALSE;
        return $query->row_array();
    }

    public function search_groups($searchTerm) {
        $this->db->select('
            group.group_id,
            group.group_name,
            group.group_description,
            user.user_id as owner_id,
            user.first_name as owner_first,
            user.last_name as owner_last,
            college.college_name as college,
            group_type.group_type_description as group_type
        ');

        $this->db->order_by('group.group_name');
        $this->db->like('group.group_name', $searchTerm);
        $this->db->join('user', 'user.user_id = group.group_owner');
        $this->db->join('college', 'college.college_id = group.group_college');
        $this->db->join('group_type', 'group_type.group_type_id = group.group_type');
        $query = $this->db->get('group');

        if ($query->num_rows() < 1) return FALSE;

       return $query->result_array();
    }

    public function get_top_ten($groupType, $groupID) {
        if ($groupType == 'group') {
            $this->db->select('
                user.user_id,
                user.first_name as first_name,
                user.last_name as last_name,
                group_roster.user_score,
                group_role.group_role_description as group_role,
            ');

            $this->db->order_by('group_roster.user_score', 'DESC');
            $this->db->join('user', 'user.user_id = group_roster.user_id');
            $this->db->join('group_role', 'group_role.group_role_id = group_roster.group_role');
            $query = $this->db->get_where('group_roster', array('group_roster.group_id' => $groupID));
        }
        else {
            $this->db->select('user_id, first_name, last_name, user_score');
            $this->db->order_by('user_score', 'DESC');
            $query = $this->db->get_where('user', array($groupType => $this->session->userdata('user')->$groupType));
        }

        if ($query->num_rows() < 1) return FALSE;

        return $query->result_array();
    }

    public function create_group($userID, $post_var) {
        if ($this->session->userdata('user')->site_role != 'faculty') return FALSE;

        $params = array(
            'group_name' => $post_var('group_name'),
            'group_description' => $post_var('group_description'),
            'group_owner' => $this->session->userdata('user')->user_id,
            'group_college' => $this->session->userdata('user')->college,
            'group_type' => 1 // *leaving this set to one until we refine the difference between the different group types
        );    
        
         if ($this->check_groupName($params['group_name'])) 
            {
                $this->db->insert('group', $params);
                
                if ($this->db->affected_rows() == 1)
                {
                    $this->session->set_flashdata('create_group_error', '<p class="error">This group has been successfully registered.</p>');
                    return TRUE;
                }
            }
            
            return FALSE;
    


    }

    /**
     * 
     * check_groupName is a helper function used to validate the group name does not already exist.
     * 
     * @name        check_groupName
     * @param       string          $group          This is the group to be validated
     * @return      bool                                    Will return TRUE if validation was successful or FALSE if it was not successful
     * 
     */
    public function check_groupName($groupName)
    {      
        $query = $this->db->get_where('group', array('group_name' => $groupName)); //  Check to see if group name exists in the 'group' table
        
        if ($query->num_rows() > 0) //  If the group name was found in either of the table return an error
        {
            $this->session->set_flashdata('create_group_error', '<p class="error">This Group Name has already been registered.</p>');
            return FALSE;
        }
        
        return TRUE;
    }

    public function getLastInserted()
    {
        $data['lastID'] = $this->db->insert_id();
        return $data;
    }

    /**
    *   get_group_officers returns a list of officers' names for a given group id
    */
    public function get_group_officers($groupType, $groupID) {
        if ($groupType == 'group') {
            $this->db->select('
                user.user_id,
                user.first_name as first_name,
                user.last_name as last_name,
                group_roster.user_score,
                group_role.group_role_description as group_role,
            ');

            $this->db->order_by('group_roster.user_score', 'DESC');
            $this->db->join('user', 'user.user_id = group_roster.user_id');
            $this->db->join('group_role', 'group_role.group_role_id = group_roster.group_role');
            $query = $this->db->get_where('group_roster', array('group_role' => 3, 'group_id' => $groupID));
        }
        else {
            $query = $this->db->get_where($groupType, array($groupType . '_id' => $groupID));
        }

        if ($query->num_rows() != 1) return FALSE;
        return $query->result_array();
    }

    /**
    *   is_group_owner returns true if userID is the owner of groupID, false otherwise
    */
    public function is_group_owner($groupType, $groupID, $userID) {
        if ($groupType == 'group') {
            $this->db->select('
                group.group_id
            ');

            $query = $this->db->get_where('group', array('group_id' => $groupID, 'group_owner' => $userID));
        }
        else {
            $query = $this->db->get_where($groupType, array($groupType . '_id' => $groupID));
        }

        if ($query->num_rows() > 0) {
            return TRUE;  
        } 
        else {
            return FALSE;  
        } 
    }

    /**
    *   get_all_members returns an array of all the members in groupID
    */
    public function get_all_members($groupID) {
        $this->db->select('
            user.user_id,
            user.first_name as first_name,
            user.last_name as last_name,
            group_roster.user_score,
            group_roster.group_role
        ');

        $this->db->order_by('group_roster.user_score', 'DESC');
        $this->db->join('user', 'user.user_id = group_roster.user_id');
        $query = $this->db->get_where('group_roster', array('group_roster.group_id' => $groupID));

        if ($query->num_rows() < 1) return FALSE;
        return $query->result_array();
    }

}